package com.dao.impl;

import com.core.model.UploadImage;
import com.core.model.lxf.Good;
import com.core.model.lxf.GoodType;
import com.core.model.lxf.SecKillGoods;
import com.core.model.lxf.SecKillOrder;
import com.core.model.user.UserInfo;
import com.dao.LxfDao;
import org.hibernate.ObjectNotFoundException;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Iterator;
import java.util.List;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-7-22
 * Time: 下午11:22
 * To change this template use File | Settings | File Templates.
 */
@Repository("lxfDao")
public class LxfDaoImpl extends EntityDaoImpl implements LxfDao {


    @Override
    public List<Good> findAllStudent() {
        String hql = "from " + Good.class.getName();
        Query query = currentSession().createQuery(hql);
        return query.list();
    }

    @Override
    public void deleteObject(Object o) {
        currentSession().delete(o);
    }

    @Override
    public UploadImage findImageByGuid(String guid) {
        String hql = "from " + UploadImage.class.getName() + " a where a.guid=:guid";
        Query query = currentSession().createQuery(hql);
        query.setCacheable(true);
        query.setParameter("guid", guid);
        List<UploadImage> list = query.list();
        if (list.isEmpty()) {
            throw new ObjectNotFoundException(guid, UploadImage.class.getName());
        }
        return list.get(0);
    }

    @Override
    public UserInfo findUserInfoByUserGuid(String userInfoGuid) {
        String hql = "from " + UserInfo.class.getName() + " a where a.guid=:guid";
        Query query = currentSession().createQuery(hql);
        query.setParameter("guid", userInfoGuid);
        List<UserInfo> list = query.list();
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public void addGood() {
        Random random = new Random();
        GoodType goodType = (GoodType) currentSession().load(GoodType.class, 1);
        for (int i = 1; i < 5001; i++) {
            Good good = new Good("goodname" + random.nextInt(), String.valueOf(random.nextInt(50)), goodType, 0);
            currentSession().save(good);
            if (i % 50 == 0) {
                currentSession().flush();
                currentSession().clear();
            }
        }
    }

    @Override
    public GoodType findGoodTypeByGuid() {
        String hql = "from " + GoodType.class.getName() + " a where a.guid=:guid";
        Query query = currentSession().createQuery(hql);
        query.setParameter("guid", "goodtypeguid");
        List<GoodType> list = query.list();
        return list.get(0);
    }

    @Override
    public List<Good> testHibernate() {
        String hql = "from " + Good.class.getName() + " a left outer join fetch a.goodType ";
//        String hql = "from " + Good.class.getName() + " a ";
        Query query = currentSession().createQuery(hql);
        return query.list();
    }

    @Override
    public List<GoodType> testGoodType() {
        String hql = "from " + GoodType.class.getName() + " ";
        Query query = currentSession().createQuery(hql);
        return query.list();
    }

    @Override
    public Good findGoodById(String id) {
        System.out.println("----- findGoodById -----");
        return findByGuid(Good.class, "cb2ed1e8-1030-4b0b-bac5-952447e103da");
    }

    public Good goodSave(Good good) {
        System.out.println("----- goodSave -----");
        Good good1 = new Good();
        good1.setId(2);
        return good1;
    }

    public void goodDeleteById(String id) {
        System.out.println("----- goodDeleteById -----");
    }

    @Override
    public int reduceStock(String id, Integer remainNum) {
        String hql = "update " + SecKillGoods.class.getName() + " g set g.remainNum=g.remainNum - :remainNum where g.id=:id and g.remainNum>0";
        Query query = currentSession().createQuery(hql);
        query.setInteger("remainNum", remainNum);
        query.setString("id", id);
        return query.executeUpdate();
    }

    @Override
    public void generateOrder(String consumer, String goodsId, Integer num) {
        SecKillGoods goods = (SecKillGoods) currentSession().load(SecKillGoods.class, Integer.parseInt(goodsId));
        currentSession().saveOrUpdate(new SecKillOrder(consumer, goods, num));
    }

    @Override
    public SecKillGoods findById(String id) {
        /*String hql = "from " + SecKillGoods.class.getName() + " a where a.id=:id";
        Query query = currentSession().createQuery(hql);
        query.setString("id", id);*/
        return (SecKillGoods) currentSession().get(SecKillGoods.class, Integer.parseInt(id));
    }


}
